package com.dao.impl;

import com.dao.ProductDao;
import com.entity.Product;
import com.utils.ConnectionUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @ProjectName: shop
 * @Author: Jsx
 * @Time: 2020/8/30 21:25
 * @Description:
 */
public class ProductDaoImpl implements ProductDao {
    @Override
    public List<Product> queryProductById(String tid, int currentpage, int pageSize) throws SQLException {
        String sql = "select p_id as pid,t_id as tid,p_name as pname,p_time as ptime ," +
                "p_image as pimage,p_state as pstate ,p_info as pinfo ,p_price as pprice " +
                "from product where t_id = ? limit ?,? ;";

        QueryRunner queryRunner = new QueryRunner(ConnectionUtils.getDataSource());

        List<Product> products = queryRunner.query(sql, new BeanListHandler<Product>(Product.class),tid,(currentpage-1)*pageSize,pageSize);

        return products;
    }

    @Override
    public Long queryCountByTid(String tid) throws SQLException {
        String sql = "select count(1) ct from product where t_id = ?";
        QueryRunner queryRunner = new QueryRunner(ConnectionUtils.getDataSource());

        Long count = (Long) queryRunner.query(sql, new ScalarHandler(), tid);

        return count;
    }
}
